package com.swsc.solarverse.dao.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swsc.solarverse.common.mybatis.BasePlusMapper;
import com.swsc.solarverse.dao.entity.DefectDO;
import com.swsc.solarverse.model.*;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-06-28
 */
@Mapper
public interface DefectMapper extends BasePlusMapper<DefectDO> {

    /**
     * 统计电站损耗
     *
     * @param queryBo
     * @return
     */
    List<CorporationDefectLossBO> statisticsCorporationDefectLoss(@Param("queryBo") DefectQueryBO queryBo);

    /**
     * 缺陷统计
     *
     * @param queryBo
     * @return
     */
    DefectSumBO defectSum(@Param("queryBo") DefectQueryBO queryBo);

    /**
     * 最新巡检时间
     *
     * @param organNo
     * @return
     */
    @Select(value = "select max(patrol_time) from solar_defect where organ_no = #{organNo}")
    LocalDateTime maxPatrolTime(@Param("organNo") Integer organNo);
    @Select(value = "select max(patrol_time) from solar_defect where organ_no = #{organNo} and fault_category = #{faultCategory}")
    LocalDateTime maxCategoryPatrolTime(@Param("organNo") Integer organNo, @Param("faultCategory") String faultCategory);

    /**
     * 重复出现次数
     *
     * @return
     */
    List<DefectRepeatSumBO> statisticsRepeatDefect(@Param("queryBo") DefectQueryBO queryBo);

    /**
     * 平均消缺时间
     *
     * @param queryBo
     * @return
     */
    Integer avgDealTime(DefectQueryBO queryBo);

    /**
     * 各型号故障统计
     *
     * @param organNoIn
     * @return
     */
    List<DefectSumBO> chartDefectSumModel(@Param("organNoIn") Collection<Integer> organNoIn);

    /**
     * 巡检统计
     *
     * @param organNoIn
     * @return
     */
    List<DefectSumBO> chartPatrolSum(@Param("organNoIn") Collection<Integer> organNoIn);

    /**
     * 消缺处理统计(天)
     *
     * @param organNoIn
     * @param start
     * @param end
     * @return
     */
    List<DefectSumBO> chartDefectSumDay(@Param("organNoIn") List<Integer> organNoIn,
                                        @Param("start") Date start,
                                        @Param("end") Date end);

    /**
     * 消缺处理统计(月)
     *
     * @param organNoIn
     * @param start
     * @param end
     * @return
     */
    List<DefectSumBO> chartDefectSumMonth(@Param("organNoIn") List<Integer> organNoIn,
                                          @Param("start") Date start,
                                          @Param("end") Date end);

    /**
     * 消缺处理统计(年)
     *
     * @param organNoIn
     * @param start
     * @param end
     * @return
     */
    List<DefectSumBO> chartDefectSumYear(@Param("organNoIn") List<Integer> organNoIn,
                                         @Param("start") Date start,
                                         @Param("end") Date end);

    /**
     * 缺陷类型统计
     *
     * @param organNoIn
     * @return
     */
    List<ChartDefectTypeSumBO> chartDefectTypeSum(@Param("organNoIn") List<Integer> organNoIn,
                                                  @Param("start") LocalDateTime startTime,
                                                  @Param("end") LocalDateTime endTime,
                                                  @Param("faultCategoryIn")Collection<String> faultCategoryIn
    );

    /**
     * 故障预警
     *
     * @param organNoIn
     * @return
     */
    List<DefectWarnStatisticsBO> defectWarn(@Param("organNoIn") List<Integer> organNoIn,
                                            @Param("start") LocalDateTime startTime,
                                            @Param("end") LocalDateTime endTime,
                                            @Param("faultCategoryIn")Collection<String> faultCategoryIn);

    /**
     * 巡检时间
     *
     * @param organNo
     * @return
     */
    @Select("select distinct organ_no, patrol_time from solar_defect where organ_no = #{organNo} and fault_category = #{faultCategory} order by patrol_time desc")
    List<PatrolTimeBO> listPatrolTime(@Param("organNo") Integer organNo, @Param("faultCategory") String faultCategory);

    /**
     * 分区
     *
     * @param organNo
     * @return
     */
    @Select("select distinct organ_no, `zone` from solar_defect where organ_no = #{organNo} order by `zone`")
    List<StationZoneBO> listZones(@Param("organNo") Integer organNo);

    /**
     * 无人机巡检统计
     *
     * @param organNoIn
     * @return
     */
    List<UavFlyRecordCountBO> uavPatrolSum(@Param("organNoIn") List<Integer> organNoIn);

    /**
     * 获取故障类型
     *
     * @param organNo
     * @param start
     * @param end
     * @return
     */
    @Select(value = "select distinct fault_name from solar_defect sd where sd.organ_no = #{organNo} and sd.patrol_time >= #{start} and sd.patrol_time < #{end} and sd.fault_category = #{faultCategory}")
    List<String> getDefectTypes(@Param("organNo") Integer organNo, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end, @Param("faultCategory")String faultCategory);

    /**
     * 故障设备统计
     * @param queryBo
     * @return
     */
    List<ChartDefectTypeSumBO> chartDefectTypeDeviceSum(@Param("queryBo")DefectQueryBO queryBo);

    List<DefectDO> listStationDefectBO(@Param("queryBo") DefectQueryBO queryBo);

    Page<DefectDO> listStationDefectBO(@Param("queryBo") DefectQueryBO queryBo,IPage<DefectDO> page);

    @MapKey(value = "id")
    DefectStatisticsBO defectStatisticsByOrganNo(@Param("organNo") Integer organNo);

    @MapKey(value = "id")
    DefectStatisticsBO defectStatistics(@Param("organNo") Integer organNo, @Param("startDate") String startDate, @Param("endDate") String endDate);

    /**
     * 分页统计设备的缺陷种类和对应次数
     */
    Page<DefectStatisticsBO> pageStatisticsDefectCount(@Param("organNo") Integer organNo, @Param("repeatTimes")Integer repeatTimes, IPage<DefectDO> page);

    /**
     * 查询
     * @param organNo
     * @return
     */
    @Select(value = "select case when model is null then '其他' else model end as moduleModel,count(*) as moduleFaultCount\n" +
            "from solar_defect where organ_no = ?1 group by model")
    List<Map<String,Object>> statisticsDefectModel(Integer organNo);


    /**
     * 分类统计故障类型和数量
     * @param timeType
     * @return
     */
    List<DefectTypeNumBO> defectType(@Param("timeType") Integer timeType,@Param("organNoList") List<Integer> organNoList);

    /**
     * 各场站故障数量统计
     * @param timeType
     * @return
     */
    List<DefectTypeNumBO> stationNumList(@Param("timeType") Integer timeType,@Param("organNoList") List<Integer> organNoList);

    /**
     * 各型号故障数量统计
     * @param timeType
     * @return
     */
    List<DefectTypeNumBO> modelNumList(@Param("timeType") Integer timeType,@Param("organNoList") List<Integer> organNoList);

    /**
     * 查询总故障数量、已消缺数量、存量故障数量
     * @param timeType
     * @return
     */
    EquipDefectBO equipDefect(@Param("timeType") Integer timeType,@Param("organNoList")List<Integer> organNoList);

    /**
     * 查询已处理的故障数量，按照统计时间维度分组统计
     * @param resType
     * @param startTime
     * @param endTime
     * @return
     */
    List<DefectTypeNumBO> defectDeal(@Param("resType")Integer resType,
                                     @Param("startTime")String startTime,
                                     @Param("endTime")String endTime,
                                     @Param("organNoList")List<Integer> organNoList);

    /**
     * 作业架次统计
     * @return
     */
    Integer getWorkSortieNum(@Param("organNoList")List<Integer> organNoList);

    /**
     * 巡检容量统计
     * @return
     */
    Double getXjCapacity(@Param("organNoList")List<Integer> organNoList);

    /**
     * 巡检组件数量
     * @return
     */
    Integer getXjModuleNum(@Param("organNoList")List<Integer> organNoList);

    /**
     * 巡检组串数量
     * @return
     */
    Integer getXjStringNum(@Param("organNoList")List<Integer> organNoList);


    /**
     * 查询任务和故障数量做导出
     * @param queryBo
     * @return
     */
    List<UavPatrolExportTaskListBO> getTaskList(@Param("queryBo")UavInspectionTaskQueryBO queryBo);

    /**
     * 同一组件多次出现（相同）故障告警
     * @param organNo
     * @param patrolTimeStrIn
     * @return
     */
    List<DefectModuleFaultBO> moduleSameFault(@Param("organNo") Integer organNo, @Param("patrolTimeStrIn") List<String> patrolTimeStrIn);

    /**
     * 同一组件多次出现相同（或者不同）故障告警--详情（两个详情共用一组数据）
     * @param organNo
     * @param patrolTimeStrIn
     * @return
     */
    List<DefectModuleFaultBO> faultDetails(@Param("organNo") Integer organNo, @Param("patrolTimeStrIn") List<String> patrolTimeStrIn);

    /**
     * 同一组件多次出现（不相同）故障告警，用GROUP_CONCAT分组合并故障类型
     * @param organNo
     * @param patrolTimeStrIn
     * @return
     */
    List<DefectModuleFaultBO> noSameFault(@Param("organNo") Integer organNo, @Param("patrolTimeStrIn") List<String> patrolTimeStrIn);

    /**
     * 根据组件编码查询当前组件的历次巡检信息
     * @param organNo
     * @param deviceModuleCode
     * @return
     */
    List<DefectDO> defectModuleInfo(@Param("organNo") Integer organNo,@Param("deviceModuleCode")  String deviceModuleCode);

    /**
     * 故障最多的组件
     * 故障最严重的组件，严重故障类型包含：破碎组件、组件失效、组件缺失、组串零电流、 组串短接
     * @return
     */
    DefectModuleFaultBO mostSeriousModule(@Param("organNo") Integer organNo, @Param("patrolTimeStrIn") List<String> patrolTimeStrIn
                                   ,@Param("isSerious")boolean isSerious);

    /**
     * 铃铛--同一组件同一类型告警（查询未消缺的数据）
     * @param organNo
     * @param patrolTimeStrIn
     * @return
     */
    DefectModuleFaultBO sameBell(@Param("organNo") Integer organNo, @Param("patrolTimeStrIn") List<String> patrolTimeStrIn);

    /**
     * 铃铛--同一组件多种类型告警（查询未消缺的数据）
     * @param organNo
     * @param patrolTimeStrIn
     * @return
     */
    DefectModuleFaultBO noSameBell(@Param("organNo") Integer organNo, @Param("patrolTimeStrIn") List<String> patrolTimeStrIn);
}
